package com.sitia.jd.controller;

import com.sitia.common.annotation.Log;
import com.sitia.common.core.controller.BaseController;
import com.sitia.common.core.domain.AjaxResult;
import com.sitia.common.core.page.TableDataInfo;
import com.sitia.common.enums.BusinessType;
import com.sitia.common.utils.poi.ExcelUtil;
import com.sitia.jd.domain.MxOrder;
import com.sitia.jd.domain.vo.MxOrderVo;
import com.sitia.jd.service.IJdService;
import com.sitia.jd.service.IMxOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * mx订单Controller
 * 
 * @author sitia
 * @date 2023-05-22
 */
@RestController
@RequestMapping("/jd/order")
public class MxOrderController extends BaseController
{
    @Autowired
    private IMxOrderService mxOrderService;
    @Resource
    private IJdService jdService;
    /**
     * 查询mx订单列表
     */
    //@PreAuthorize("@ss.hasPermi('jd:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(MxOrder mxOrder)
    {
        startPage();
        List<MxOrder> list = mxOrderService.selectMxOrderList(mxOrder);
        return getDataTable(list);
    }

    /**
     * 查询mx订单列表【网页列表，有权限控制】
     */
    @GetMapping("/pageList")
    public TableDataInfo pageList(MxOrder mxOrder)
    {
        startPage();
        List<MxOrder> list = mxOrderService.selectMxOrderPageListByPermission(mxOrder);
        return getDataTable(list);
    }

    /**
     * 导出mx订单列表
     */
    //@PreAuthorize("@ss.hasPermi('jd:order:export')")
    @Log(title = "mx订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MxOrder mxOrder)
    {
        List<MxOrder> list = mxOrderService.selectMxOrderList(mxOrder);
        ExcelUtil<MxOrder> util = new ExcelUtil<MxOrder>(MxOrder.class);
        util.exportExcel(response, list, "mx订单数据");
    }

    /**
     * 获取mx订单详细信息
     */
    //@PreAuthorize("@ss.hasPermi('jd:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(mxOrderService.selectMxOrderById(id));
    }

    /**
     * 新增mx订单
     */
    //@PreAuthorize("@ss.hasPermi('jd:order:add')")
    @Log(title = "添加mx订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MxOrder mxOrder)
    {
        return toAjax(mxOrderService.insertMxOrder(mxOrder));
    }

    /**
     * 修改mx订单
     */
    //@PreAuthorize("@ss.hasPermi('jd:order:edit')")
    @Log(title = "修改mx订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MxOrder mxOrder)
    {
        return toAjax(mxOrderService.updateMxOrder(mxOrder));
    }

    /**
     * 删除mx订单
     */
    //@PreAuthorize("@ss.hasPermi('jd:order:remove')")
    @Log(title = "删除mx订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(mxOrderService.deleteMxOrderByIds(ids));
    }

    /**
     * 新增mx订单
     */
    @Log(title = "添加mx订单", businessType = BusinessType.INSERT)
    @PostMapping("/userAdd")
    public AjaxResult userAdd(@RequestBody MxOrder mxOrder)
    {
        return toAjax(mxOrderService.insertUserOrder(mxOrder));
    }


    /**
     * 完成订单接口 （根据Cookie完成订单）
     */
    @PostMapping("/completeOrder")
    public AjaxResult completeOrder(@RequestBody MxOrder mxOrder)
    {
        return toAjax(mxOrderService.completeOrder(mxOrder));
    }

    /**
     * 从管理员登陆界面生成订单
     */
    @Log(title = "添加免息订单", businessType = BusinessType.INSERT)
    @PostMapping("/addAdminOrder")
    public AjaxResult addAdminOrder(@RequestBody MxOrderVo mxOrder)
    {
        return toAjax(mxOrderService.addAdminOrder(mxOrder));
    }


    /**
     * 查询根据用户查询订单列表
     */
    //@PreAuthorize("@ss.hasPermi('jd:order:list')")
    @GetMapping("/getOrderList")
    public TableDataInfo getOrderList(MxOrder mxOrder)
    {

        startPage();
        List<MxOrder> list = mxOrderService.selectMxOrderListByAuthority(mxOrder);
        return getDataTable(list);
    }

    /**
     * 更新当前订单cookie的最新状态
     * @param mxOrder
     * @return
     */
    @PostMapping("/updateOrderLoginStatus")
    public AjaxResult updateOrderLoginStatus(@RequestBody MxOrder mxOrder)
    {
        return toAjax(mxOrderService.updateOrderLoginStatus(mxOrder));
    }


    /**
     * 根据Cookie获取账户优惠券列表
     * @param mxOrder
     * @return
     */
    @PostMapping("/getCouponList")
    public AjaxResult getCouponList(@RequestBody MxOrder mxOrder)
    {
        return AjaxResult.success(mxOrderService.getCouponList(mxOrder));
    }

    /**
     * 改变是否添加返利状态接口 （根据id完成订单）
     */
    @PostMapping("/changAddRebate")
    public AjaxResult changAddRebate(@RequestBody MxOrder mxOrder)
    {
        return toAjax(mxOrderService.changAddRebate(mxOrder));
    }

    /**
     * 修改为添加返利状态 （根据cookie修改为添加返利）
     */
    @PostMapping("/changAddRebateByCookie")
    public AjaxResult changAddRebateStatusByCookie(@RequestBody MxOrder mxOrder)
    {
        return toAjax(mxOrderService.changAddRebateStatusByCookie(mxOrder));
    }
    /**
     * 获取mx订单详细信息（详细界面接口）
     */
    //@PreAuthorize("@ss.hasPermi('jd:order:query')")
    @GetMapping(value = "/getOrderDetail/{id}")
    public AjaxResult getOrderDetail(@PathVariable("id") String id)
    {
        return AjaxResult.success(mxOrderService.getOrderDetail(id));
    }

    /**
     * 刷新今日抢购的cookie状态
     * @return
     */
    @GetMapping(value = "/refreshTodayCookieStatus")
    public AjaxResult refreshTodayCookieStatus()
    {
        mxOrderService.refreshTodayCookieStatus();
        return AjaxResult.success();
    }

    /**
     * 批量更新
     *
     * @param vo
     * @return
     */
    @PostMapping("/batchUpdate")
    public AjaxResult batchUpdate(@RequestBody MxOrderVo vo) {

        mxOrderService.batchUpdate(vo);
        return AjaxResult.success();
    }

    /**
     * 获取返利链接
     */
    @PostMapping("/getRebateUrl")
    public AjaxResult getRebateUrl(@RequestBody MxOrder mxOrder) {
        return jdService.getRebateUrl(mxOrder);
    }
}



